package com.woniuxy.controller.dailycontroller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.woniuxy.pojo.Break;
import com.woniuxy.pojo.Breakpicture;
import com.woniuxy.pojo.Carpicture;
import com.woniuxy.pojo.Cars;
import com.woniuxy.service.basics.BookService;
import com.woniuxy.service.basics.CarService;
import com.woniuxy.service.basics.DriverService;
import com.woniuxy.service.daily.BreakService;
import com.woniuxy.service.daily.BreakpictureService;

@Controller
@RequestMapping("break")
public class BreakController {
	@Autowired
	private CarService carService;
	
	@Autowired
	private BreakService breakService;
	
	@Autowired
	private BookService bookService;
	
	@Autowired
	private DriverService driverService;
	
	@Autowired
	private BreakpictureService breakpictureService;
	
	@RequestMapping("getAll")
	@ResponseBody
	public PageInfo getAll(Integer pn,String condition) {
		PageInfo pageInfo = breakService.selectAll(pn,condition);
		return pageInfo;
	}

	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String carIds) {
		int deleteCars = breakService.deleteBreak(carIds);
		return "成功删除"+deleteCars+"条记录";
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Map<String, List<String>> add(String carIds) {
		List<String> brCarlicense = carService.selectAllCarlicense();
		List<String> brcontent = bookService.selectByParentName("违章项目");
		List<String> brdriver = driverService.selectAllDriverName();
		Map<String,List<String>> map = new HashMap<String, List<String>>();
		map.put("brCarlicense", brCarlicense);
		map.put("brcontent", brcontent);
		map.put("brdriver", brdriver);
		return map;
	}
	
	@RequestMapping("doUpdate")
	@ResponseBody
	public String doUpdate(Break breaks) {
		breakService.updateBreak(breaks);
		return "";
	}
	
	@RequestMapping("doAdd")
	@ResponseBody
	public String doAdd(Break breaks) {
		breakService.addBreak(breaks);
		return "";
	}
	
	@RequestMapping("update")
	@ResponseBody
	public Map<String, Object> update(Integer carId) {
		Break breaks = breakService.selectById(carId);
		List<String> brCarlicense = carService.selectAllCarlicense();
		List<String> brcontent = bookService.selectByParentName("违章项目");
		List<String> brdriver = driverService.selectAllDriverName();
		Map<String,Object> map = new HashMap();
		map.put("brCarlicense", brCarlicense);
		map.put("brcontent", brcontent);
		map.put("brdriver", brdriver);
		map.put("breaks", breaks);
		return map;
	}
	@RequestMapping("/showInfo")
	@ResponseBody
	public Break showInfo(Integer carId) {
		return breakService.selectById(carId);
	}
	
	@RequestMapping("/showPicture")
	public String showPicture(Model model,Integer brId) {
		List<Breakpicture> list = breakpictureService.selectBybrId(brId);
		HashMap<String,Object> map = new HashMap();
		map.put("brId", brId);
		map.put("list", list);
		model.addAllAttributes(map);
		return "/daily/breakpicture.jsp";
	}
	
	
	@RequestMapping("/delPicture")
	public String delPicture(Model model,Integer brId,Integer bpId) {
		model.addAttribute("carId", brId);
		breakpictureService.deletePicture(bpId);
		return "/car/showPicture";
	}
	
	@RequestMapping("/uploadPicture")
	public String uploadPicture(MultipartFile breakimg,HttpServletRequest req,Integer brId,String bpDesc,Model model) {
		String realPath = req.getServletContext().getRealPath("/");
		System.out.println(realPath);
		File uploadDir = new File(realPath,"upload");
		if (!uploadDir.exists()) {
			uploadDir.mkdir();
		}
		String newFileName=System.currentTimeMillis()+breakimg.getOriginalFilename().substring(breakimg.getOriginalFilename().indexOf("."));
		try {
			breakimg.transferTo(new File(uploadDir,newFileName));
			Breakpicture breakpicture = new Breakpicture();
			breakpicture.setbrId(brId);
			breakpicture.setbpDesc(bpDesc);
			breakpicture.setbpFilename("upload/"+newFileName);
			breakpictureService.addPicture(breakpicture);
			model.addAttribute("carId",brId);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "/break/showPicture";
	}
	
}
